package com.zjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.domain.entity.GroupMessage;
import com.zjw.domain.vo.GroupMessageVo;
import com.zjw.domain.vo.ResponseResult;
import com.zjw.mapper.GroupMessageMapper;
import com.zjw.service.GroupMessageService;
import com.zjw.service.UserService;
import com.zjw.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 群聊消息表(GroupMessage)表服务实现类
 *
 * @author lkzy
 * @since 2024-01-03 14:41:29
 */
@Service("groupMessageService")
public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper, GroupMessage> implements GroupMessageService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult getAll() {

        //    按创建时间降序
        LambdaQueryWrapper<GroupMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(GroupMessage::getCreateTime);

        List<GroupMessage> tempList = list(wrapper);

        List<GroupMessageVo> res = BeanCopyUtil.copyList(tempList, GroupMessageVo.class);

        // 获取创建者的头像和用户名
        res.stream().forEach(m->{
            Long temp = m.getUserId();
            m.setUsername(userService.getUserNameById(temp));
            m.setAvatar(userService.getUserAvatarById(temp));
        });

        return ResponseResult.ok(res);
    }
}